package com.example.zhihuhelpapp.utils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class SpUtils {
    private static SpUtils instance;
    private SharedPreferences preferences;
    private SpUtils() {
        preferences = PreferenceManager.getDefaultSharedPreferences(CoreLib.getContext());
    }

    /**
     * 单例模式的SharedPreferencesUtil
     */
    public static SpUtils getInstance() {
        if (instance == null) {
            synchronized (SpUtils.class) {
                if (instance == null) {
                    instance = new SpUtils();
                }
            }
        }
        return instance;
    }

    public void put(String key, String value) {
        SharedPreferences.Editor edit = preferences.edit();
        if (edit != null) {
            edit.putString(key, value);
            edit.apply();
        }
    }

    public String get(String key, String defValue) {
        return preferences.getString(key, defValue);
    }
}
